package org.study.openapi.baidu;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class BaduApiYoudaoActive {
    private static final String headUrl = "https://fanyi-api.baidu.com/api/trans/vip/translate?%s";
    private static final String appid = "20200219000385879";
    private static final String secret = "5KbLE2p8FkxMdLkjFYRq";
    public static void main(String[] args) {

        String q = "window";
        String salt = IdUtil.fastSimpleUUID();
        String sign = SecureUtil.md5(new StringBuilder(appid).append(q).append(salt).append(secret).toString()).toLowerCase();
        Map<String,Object> param = new HashMap<>();
        param.put("q", URLUtil.encode(q));
        param.put("from","auto");
        param.put("to","zh");
        param.put("appid",appid);
        param.put("salt",salt);
        param.put("sign",sign);
        HttpRequest get = HttpUtil.createGet(String.format(headUrl,URLUtil.buildQuery(param, Charset.forName("utf-8"))));
        Console.log(get.execute().charset("utf-8").body());
    }

}
